from userapp.models import UserProfile
from rest_framework import serializers
from .models import *
from goodsapp.models import GoodsHistory
from goodsapp.sers import GoodsListSer


class UserDetailSer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ("username", "phone", "email")


class CitySer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields = ["id", "name", "parentid"]


class AddressSer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = "__all__"


class AddressListSer(serializers.ModelSerializer):
    province_id = serializers.SerializerMethodField()
    city_id = serializers.SerializerMethodField()
    district_id = serializers.SerializerMethodField()

    def get_province_id(self, addr):
        return City.objects.get(id=addr.province_id).name

    def get_city_id(self, addr):
        return City.objects.get(id=addr.city_id).name

    def get_district_id(self, addr):
        return City.objects.get(id=addr.district_id).name

    class Meta:
        model = Address
        fields = "__all__"


class GoodsHistorySer(serializers.ModelSerializer):
    class Meta:
        model = GoodsHistory
        fields = "__all__"


class GoodsHistoryListSer(serializers.ModelSerializer):
    goods = GoodsListSer()

    class Meta:
        model = GoodsHistory
        fields = "__all__"
